﻿using Dapper;
using SassModule.Contracts;
using SassModule.Entity;
using SassModule.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

namespace SassModule.Services
{
    /// <summary>
    /// 账套服务
    /// </summary>
    public class SassService : ISassService
    {
        /// <summary>
        /// 新建账套
        /// </summary>
        /// <param name="info"></param>
        public void Add(Sass info)
        {
            //生成一个数据库名字
            var suffix = (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds;
            info.DB = $"soadb{suffix}";
            using var db = SassModule.ConnCache.GetConn();
            //创建数据库
            if (File.Exists("soadb.sql"))
            {
                db.Execute($"CREATE DATABASE {info.DB};");
                Thread.Sleep(TimeSpan.FromSeconds(1));
                var sql = File.ReadAllText("soadb.sql");
                sql= sql.Replace("{db}", info.DB);
                db.Execute(sql);
            }
            info.UserName = $"admin{suffix}";
            info.Password = "123456";
            db.Execute("INSERT INTO dbo.Sys_Sass(Id,Name,UserName,Password,Mobile,Email,Des,CreateTime,Description,Status,DB,CompanyName,TaxNum,Contact,Bank,BankNum,Remark,Expire) VALUES(NEWID(),@Name,@UserName,@Password,@Mobile,@Email,@Des,GETDATE(),@Description,'NORMAL',@DB,@CompanyName,@TaxNum,@Contact,@Bank,@BankNum,@Remark,@Expire)", info);
        }
        /// <summary>
        /// 设置账套Logo和Title
        /// </summary>
        /// <param name="data"></param>
        /// <param name="dbStr"></param>
        public void ConfigLogoAndTitle(SassConfigViewModel data, string dbStr=null)
        {
            using var db = SassModule.ConnCache.GetConn();
            db.Execute("UPDATE Sys_Sass SET Title=@title,Img=@img WHERE DB=@dbStr", new { title =data.Title, img =data.Img, dbStr });
        }

        /// <summary>
        /// 更新Sass
        /// </summary>
        /// <param name="info"></param>
        public void UpdateSass(Sass info)
        {
            if (info.Id == null) return;
            //写sql语句
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("UPDATE Sys_Sass SET");
            if (info.Name !=null)
            {
                sql.AppendLine("[Name] = @Name,");
            }
            if (info.Password!=null)
            {
                sql.AppendLine("[Password] = @Password,");
            }
            if (info.Email!=null)
            {
                sql.AppendLine("[Email] = @Email,");
            }
            if (info.Mobile!=null)
            {
                sql.AppendLine("[Mobile] = @Mobile,");
            }
           
            if (info.Des!=null)
            {
                sql.AppendLine("[Des] = @Des,");
            }
           
            if (info.Description!=null)
            {
                sql.AppendLine("[Description] = @Description,");
            }
            if (info.Status!=null)
            {
                sql.AppendLine("[Status] = @Status,");
            }
            if (info.DB!=null)
            {
                sql.AppendLine("[DB] = @DB,");
            }
            if (info.CompanyName!=null)
            {
                sql.AppendLine("[CompanyName] = @CompanyName,");
            }
            if (info.TaxNum!=null)
            {
                sql.AppendLine("[TaxNum] = @TaxNum,");
            }
            if (info.Contact!=null)
            {
                sql.AppendLine("[Contact] = @Contact,");
            }
            if (info.Bank!=null)
            {
                sql.AppendLine("[Bank] = @Bank,");
            }
            if (info.BankNum!=null)
            {
                sql.AppendLine("[BankNum] = @BankNum,");
            }
            if (info.Remark!=null)
            {
                sql.AppendLine("[Remark] = @Remark,");
            }
            if (info.Expire!=null)
            {
                sql.AppendLine("[Expire] = @Expire,");
            }

            sql.AppendLine("[UserName] = @UserName");
            sql.AppendLine("WHERE [id]=@id");
            using var db = SassModule.ConnCache.GetConn();
            db.Execute(sql.ToString(),info);
        }

        /// <summary>
        /// 根据Id删除账套
        /// </summary>
        /// <param name="id"></param>
        public void Del(Guid id)
        {
            using var db = SassModule.ConnCache.GetConn();
            db.Execute("DELETE FROM Sys_Sass WHERE Id=@id", new { id});
        }
        /// <summary>
        /// 账套下拉框
        /// </summary>
        /// <returns></returns>
        public IEnumerable<DownListInfo> DownList()
        {
            using var db = SassModule.ConnCache.GetConn();
            var list = db.Query<DownListInfo>("SELECT  DB AS Value,Name AS Name FROM Sys_Sass WHERE Status='NORMAL' ").ToList();
            list.Add(new DownListInfo() { Name = "Admin", Value = "SOADB" });
            return list;
        }
        /// <summary>
        /// 获取所有的账套
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Sass> Index()
        {
            using var db = SassModule.ConnCache.GetConn();
            return db.Query<Sass>("SELECT [Id],[Name],[UserName],[Password],[Mobile],[Email],[Des],[AllowUsers],[AllowMachines],[CreateTime],[Description],[Status],[DB],[CompanyName],[TaxNum],[Contact],[Bank],[BankNum],[Remark],[Expire]  FROM [dbo].[Sys_Sass]");
        }
        /// <summary>
        /// 重置账套密码
        /// </summary>
        /// <param name="sass"></param>
        public void Reset(SassPasswordViewModel sass)
        {
            using var db = SassModule.ConnCache.GetConn();
            db.Execute("UPDATE Sys_Sass SET Password=@Password WHERE Id=@Id", sass);
        }
        /// <summary>
        /// 账套启用禁用
        /// </summary>
        /// <param name="sass"></param>
        public void Switch(SassViewModel sass)
        {
            using var db = SassModule.ConnCache.GetConn();
            db.Execute("UPDATE Sys_Sass SET Status=@Status WHERE Id=@Id",sass);
        }
    }
}
